package com.yishan.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yishan.entity.SysOrg;
import com.yishan.entity.SysProcess;
import com.yishan.entity.SysUser;
import com.yishan.mapper.SysOrgMapper;
import com.yishan.mapper.SysProcessMapper;
import com.yishan.mapper.SysUserMapper;
import com.yishan.service.FlowService;
import liquibase.util.ObjectUtil;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Map;

@Service
public class FlowServiceImpl implements FlowService {
    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private SysProcessMapper sysProcessMapper;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysOrgMapper sysOrgMapper;

    @Override
    public void deployment(File file) throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream(file);
        repositoryService.createDeployment()
                .addInputStream("测试流程.bpmn", fileInputStream)
                .deploy();
    }

    @Override
    public void deleteProcessDefinition(String processDefinitionId) {
        repositoryService.deleteDeployment(processDefinitionId);
    }

    @Override
    public String startProcess(String processCode, Map<String, Object> variables) {
        SysProcess sysProcess = sysProcessMapper.selectOne(new LambdaQueryWrapper<SysProcess>()
                .eq(SysProcess::getProcessCode, processCode));
        if (ObjectUtils.isEmpty(sysProcess)){
            return "当前流程不存在";
        }
        String processId = sysProcess.getProcessId();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processId,variables);
        String processInstanceId = processInstance.getProcessInstanceId();
        return "流程发起成功,流程实例ID："+processInstanceId;
    }

    @Override
    public String newGroup(SysOrg sysOrg) {
        int insert = sysOrgMapper.insert(sysOrg);
        Group group = identityService.newGroup("P:" + insert);
        group.setName(sysOrg.getOrgName());
        identityService.saveGroup(group);
        return "新增部门成功,部门ID："+insert;
    }

    @Override
    public String newUser(SysUser sysUser) {
        int insert = sysUserMapper.insert(sysUser);
        User user = identityService.newUser(String.valueOf(insert));
        user.setFirstName(sysUser.getUserName());
        user.setLastName("");
        identityService.saveUser(user);
        identityService.createMembership(String.valueOf(insert),"P:"+ sysUser.getOrgId());
        return "新增用户成功,用户ID："+insert;
    }
}
